using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;

namespace VISS.API.Events {
	class NoelEventDA {

        private const string SP_GetByUserKey = "dbo.[_NoelEvent_GetByUserKey]";
        private const string SP_Insert = "dbo.[_NoelEvent_Insert]";
        private const string SP_Update = "dbo.[_NoelEvent_Update]";

		public static NoelEventEnt CreateFromReader(IDataReader reader) {
			NoelEventEnt item = new NoelEventEnt();
			item.UserKey = ConvertUtils.ToString(reader["UserKey"]);
			item.GoOut = ConvertUtils.ToBool(reader["GoOut"]);
			return item;
		}

        public static NoelEventEnt GetByUserKey(string userKey, string connectioName, DbConnection dbCon, DbTransaction dbTrans) {
            NoelEventEnt result = null;
            Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_GetByUserKey)) {
                db.AddParameter(cmd, "UserKey", userKey);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    if (reader.Read()) {
                        result = CreateFromReader(reader);
                    }
                }
            }
            return result;
        }

        public static int Insert(NoelEventEnt item, string connectioName , DbConnection dbCon, DbTransaction dbTrans) {
            int result = -1;
            Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_Insert)) {
                db.AddParameter(cmd, "UserKey", item.UserKey);
                db.AddParameter(cmd, "GoOut", item.GoOut);

                if (dbCon != null && dbTrans != null) {
                    result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
                }
                else {
                    result = db.ExecuteNonQuery(cmd);
                }
            }
            return result;
        }

        public static int Update(NoelEventEnt item, string connectioName, DbConnection dbCon, DbTransaction dbTrans) {
            int result = -1;
            Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_Update)) {
                db.AddParameter(cmd, "UserKey", item.UserKey);
                db.AddParameter(cmd, "GoOut", item.GoOut);

                if (dbCon != null && dbTrans != null) {
                    result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
                }
                else {
                    result = db.ExecuteNonQuery(cmd);
                }
            }
            return result;
        }
	}
}